﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.ApplicationBlocks.Data;

namespace DanhmucThietbi
{
    public class ThietBiInfo
    {
        private string _MA_DONVI;

        public string MA_DONVI
        {
            get { return _MA_DONVI; }
            set { _MA_DONVI = value; }
        }
        private string _MA_TB;

        public string MA_TB
        {
            get { return _MA_TB; }
            set { _MA_TB = value; }
        }

        private string _TEN_TB;

        public string TEN_TB
        {
            get { return _TEN_TB; }
            set { _TEN_TB = value; }
        }

        private string _MA_PLOAI;

        public string MA_PLOAI
        {
            get { return _MA_PLOAI; }
            set { _MA_PLOAI = value; }
        }

        private DateTime? _NGAY_SX;

        public DateTime? NGAY_SX
        {
            get { return _NGAY_SX; }
            set { _NGAY_SX = value; }
        }

        private string _MA_NHACC;

        public string MA_NHACC
        {
            get { return _MA_NHACC; }
            set { _MA_NHACC = value; }
        }

        private string _THONGTIN_TB;

        public string THONGTIN_TB
        {
            get { return _THONGTIN_TB; }
            set { _THONGTIN_TB = value; }
        }

        private DateTime? _NGAY_MUA;

        public DateTime? NGAY_MUA
        {
            get { return _NGAY_MUA; }
            set { _NGAY_MUA = value; }
        }

        private DateTime? _NGAY_SDUNG;

        public DateTime? NGAY_SDUNG
        {
            get { return _NGAY_SDUNG; }
            set { _NGAY_SDUNG = value; }
        }

        private int? _NGUYEN_GIA;

        public int? NGUYEN_GIA
        {
            get { return _NGUYEN_GIA; }
            set { _NGUYEN_GIA = value; }
        }

        private int? _THOIGIAN_BHANH;

        public int? THOIGIAN_BHANH
        {
            get { return _THOIGIAN_BHANH; }
            set { _THOIGIAN_BHANH = value; }
        }

        private bool _IS_LKIEN;

        public bool IS_LKIEN
        {
            get { return _IS_LKIEN; }
            set { _IS_LKIEN = value; }
        }

        private string _NGUOI_TAO;

        public string NGUOI_TAO
        {
            get
            {
                if (_NGUOI_TAO == null) _NGUOI_TAO = "ADMIN";
                return _NGUOI_TAO;
            }
            set { _NGUOI_TAO = value; }
        }

        private DateTime? _NGAY_TAO;

        public DateTime? NGAY_TAO
        {
            get { return _NGAY_TAO; }
            set { _NGAY_TAO = value; }
        }

        private string _NGUOI_SUA;

        public string NGUOI_SUA
        {
            get
            {
                if (_NGUOI_SUA == null) _NGUOI_SUA = "ADMIN";
                return _NGUOI_SUA;
            }
            set { _NGUOI_SUA = value; }
        }

        private DateTime? _NGAY_SUA;

        public DateTime? NGAY_SUA
        {
            get { return _NGAY_SUA; }
            set { _NGAY_SUA = value; }
        }

        private bool _IS_DELETE;

        public bool IS_DELETE
        {
            get { return _IS_DELETE; }
            set { _IS_DELETE = value; }
        }

        private string _MA_LIENKET;

        public string MA_LIENKET
        {
            get { return _MA_LIENKET; }
            set { _MA_LIENKET = value; }
        }
    }

    class CtrThietBi
    {
        string myCnn = "";
        public CtrThietBi()
        {
            myCnn = ConfigurationManager.ConnectionStrings["myConnectionString"].ToString();
        }

        public List<ThietBiInfo> GetAll()
        {
            List<ThietBiInfo> returnList = new List<ThietBiInfo>();
            SqlDataReader ObjRead = SqlHelper.ExecuteReader(myCnn, CommandType.StoredProcedure, "DEV_SP_THIETBI_GetAllItem");
            while (ObjRead.Read())
            {
                ThietBiInfo ObjThietBi = new ThietBiInfo();
                ObjThietBi.MA_TB = ObjRead["MA_TB"].ToString();
                ObjThietBi.TEN_TB = ObjRead["TEN_TB"].ToString();
                ObjThietBi.MA_PLOAI = ObjRead["MA_PLOAI"].ToString();
                ObjThietBi.NGAY_SX = (DateTime)ObjRead["NGAY_SX"];
                ObjThietBi.MA_NHACC = ObjRead["MA_NHACC"].ToString();
                ObjThietBi.THONGTIN_TB = ObjRead["THONGTIN_TB"].ToString();
                ObjThietBi.NGAY_MUA = (DateTime)ObjRead["NGAY_MUA"];
                ObjThietBi.NGAY_SDUNG = (DateTime)ObjRead["NGAY_SDUNG"];
                ObjThietBi.NGUYEN_GIA = (int)ObjRead["NGUYEN_GIA"];
                ObjThietBi.THOIGIAN_BHANH = (int)ObjRead["THOIGIAN_BHANH"];
                ObjThietBi.NGUOI_TAO = ObjRead["NGUOI_TAO"].ToString();
                ObjThietBi.MA_LIENKET = ObjRead["MA_LIENKET"].ToString();
                returnList.Add(ObjThietBi);
            }
            return returnList;
        }

        public List<ThietBiInfo> GetAllTB()
        {
            List<ThietBiInfo> returnList = new List<ThietBiInfo>();
            SqlDataReader ObjRead = SqlHelper.ExecuteReader(myCnn, CommandType.StoredProcedure, "DEV_SP_THIETBI_GetAllItemTB");
            while (ObjRead.Read())
            {
                ThietBiInfo ObjThietBi = new ThietBiInfo();
                ObjThietBi.MA_TB = ObjRead["MA_TB"].ToString();
                ObjThietBi.TEN_TB = ObjRead["TEN_TB"].ToString();
                ObjThietBi.MA_PLOAI = ObjRead["MA_PLOAI"].ToString();
                ObjThietBi.NGAY_SX = (DateTime)ObjRead["NGAY_SX"];
                ObjThietBi.MA_NHACC = ObjRead["MA_NHACC"].ToString();
                ObjThietBi.THONGTIN_TB = ObjRead["THONGTIN_TB"].ToString();
                ObjThietBi.NGAY_MUA = (DateTime)ObjRead["NGAY_MUA"];
                ObjThietBi.NGAY_SDUNG = (DateTime)ObjRead["NGAY_SDUNG"];
                ObjThietBi.NGUYEN_GIA = (int)ObjRead["NGUYEN_GIA"];
                ObjThietBi.THOIGIAN_BHANH = (int)ObjRead["THOIGIAN_BHANH"];
                ObjThietBi.NGUOI_TAO = ObjRead["NGUOI_TAO"].ToString();
                ObjThietBi.MA_LIENKET = ObjRead["MA_LIENKET"].ToString();
                returnList.Add(ObjThietBi);
            }
            return returnList;
        }

        public DataTable GetTB()
        {
            return SqlHelper.ExecuteDataset(myCnn, CommandType.StoredProcedure, "DEV_SP_THIETBI_GetAllItemTB").Tables[0];
        }

        public DataTable GetLK()
        {
            return SqlHelper.ExecuteDataset(myCnn, CommandType.StoredProcedure, "DEV_SP_THIETBI_GetAllItem").Tables[0];
        }

        public ThietBiInfo GetByID(string MaTB)
        {
            SqlDataReader ObjRead = SqlHelper.ExecuteReader(myCnn, CommandType.StoredProcedure, "DEV_SP_THIETBI_GetByID", new SqlParameter("@MA_TB", MaTB));
            ThietBiInfo ObjThietBi = null;
            while (ObjRead.Read())
            {
                ObjThietBi = new ThietBiInfo();
                ObjThietBi.MA_TB = ObjRead["MA_TB"].ToString();
                ObjThietBi.TEN_TB = ObjRead["TEN_TB"].ToString();
                ObjThietBi.MA_PLOAI = ObjRead["MA_PLOAI"].ToString();
                ObjThietBi.NGAY_SX = (DateTime)ObjRead["NGAY_SX"];
                ObjThietBi.MA_NHACC = ObjRead["MA_NHACC"].ToString();
                ObjThietBi.THONGTIN_TB = ObjRead["THONGTIN_TB"].ToString();
                ObjThietBi.NGAY_MUA = (DateTime)ObjRead["NGAY_MUA"];
                ObjThietBi.NGAY_SDUNG = (DateTime)ObjRead["NGAY_SDUNG"];
                ObjThietBi.NGUYEN_GIA = (int)ObjRead["NGUYEN_GIA"];
                ObjThietBi.THOIGIAN_BHANH = (int)ObjRead["THOIGIAN_BHANH"];
                ObjThietBi.NGUOI_TAO = ObjRead["NGUOI_TAO"].ToString();
                ObjThietBi.MA_LIENKET = ObjRead["MA_LIENKET"].ToString();
            }
            return ObjThietBi;
        }

        public void InsertItem(ThietBiInfo Item)
        {
            SqlParameter[] Para = new SqlParameter[13];
            Para[0] = new SqlParameter("MA_TB", Item.MA_TB);
            Para[1] = new SqlParameter("TEN_TB", Item.TEN_TB);
            Para[2] = new SqlParameter("MA_PLOAI", Item.MA_PLOAI);
            Para[3] = new SqlParameter("NGAY_SX", Item.NGAY_SX == null ? (object)DBNull.Value : Item.NGAY_SX);
            Para[4] = new SqlParameter("NHA_CC", Item.MA_NHACC == null ? (object)DBNull.Value : Item.MA_NHACC);
            Para[5] = new SqlParameter("THONGTIN_TB", Item.THONGTIN_TB == null ? (object)DBNull.Value : Item.THONGTIN_TB);
            Para[6] = new SqlParameter("NGAY_MUA", Item.NGAY_MUA == null ? (object)DBNull.Value : Item.NGAY_MUA);
            Para[7] = new SqlParameter("NGAY_SDUNG", Item.NGAY_SDUNG == null ? (object)DBNull.Value : Item.NGAY_SDUNG);
            Para[8] = new SqlParameter("NGUYEN_GIA", Item.NGUYEN_GIA == null ? (object)DBNull.Value : Item.NGUYEN_GIA);
            Para[9] = new SqlParameter("THOIGIAN_BHANH", Item.THOIGIAN_BHANH == null ? (object)DBNull.Value : Item.THOIGIAN_BHANH);
            Para[10] = new SqlParameter("NGUOI_TAO", Item.NGUOI_TAO);
            Para[11] = new SqlParameter("MA_LIENKET", Item.MA_LIENKET == null ? (object)DBNull.Value : Item.MA_LIENKET);
            Para[12] = new SqlParameter("IS_LKIEN", Item.IS_LKIEN);
            try
            {
                SqlHelper.ExecuteNonQuery(myCnn, CommandType.StoredProcedure, "DEV_SP_THIETBI_Insert", Para);
            }
            catch (Exception)
            {
                throw new System.ArgumentException(string.Format("Mã thiết bị {0} đã tồn tại, vui lòng chọn mã khác", Item.MA_TB));
            }
        }

        public void UpdateItem(ThietBiInfo Item)
        {
            SqlParameter[] Para = new SqlParameter[12];
            Para[0] = new SqlParameter("MA_TB", Item.MA_TB);
            Para[1] = new SqlParameter("TEN_TB", Item.TEN_TB);
            Para[2] = new SqlParameter("MA_PLOAI", Item.MA_PLOAI);
            Para[3] = new SqlParameter("NGAY_SX", Item.NGAY_SX == null ? (object)DBNull.Value : Item.NGAY_SX);
            Para[4] = new SqlParameter("NHA_CC", Item.MA_NHACC == null ? (object)DBNull.Value : Item.MA_NHACC);
            Para[5] = new SqlParameter("THONGTIN_TB", Item.THONGTIN_TB == null ? (object)DBNull.Value : Item.THONGTIN_TB);
            Para[6] = new SqlParameter("NGAY_MUA", Item.NGAY_MUA == null ? (object)DBNull.Value : Item.NGAY_MUA);
            Para[7] = new SqlParameter("NGAY_SDUNG", Item.NGAY_SDUNG == null ? (object)DBNull.Value : Item.NGAY_SDUNG);
            Para[8] = new SqlParameter("NGUYEN_GIA", Item.NGUYEN_GIA == null ? (object)DBNull.Value : Item.NGUYEN_GIA);
            Para[9] = new SqlParameter("THOIGIAN_BHANH", Item.THOIGIAN_BHANH == null ? (object)DBNull.Value : Item.THOIGIAN_BHANH);
            Para[10] = new SqlParameter("NGUOI_SUA", Item.NGUOI_SUA);
            Para[11] = new SqlParameter("MA_LIENKET", Item.MA_LIENKET == null ? (object)DBNull.Value : Item.MA_LIENKET);
            try
            {
                SqlHelper.ExecuteNonQuery(myCnn, CommandType.StoredProcedure, "DEV_SP_THIETBI_Update", Para);
            }
            catch (Exception)
            {
                throw;
            }

        }

        public void DeleteItem(ThietBiInfo Item)
        {
            SqlParameter[] Para = new SqlParameter[2];
            Para[0] = new SqlParameter("MA_TB", Item.MA_TB);
            Para[1] = new SqlParameter("NGUOI_SUA", Item.NGUOI_SUA);
            SqlHelper.ExecuteNonQuery(myCnn, CommandType.StoredProcedure, "DEV_SP_THIETBI_Delete", Para);
        }

        public DataTable GetLKByTBID(string strMa_tb)
        {
            return  SqlHelper.ExecuteDataset(myCnn, CommandType.StoredProcedure, "DEV_SP_THIETBI_getListByLK", new SqlParameter("MA_LienKet", strMa_tb)).Tables[0];

        }
    }
}
